import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator implements ActionListener {
    private final JFrame frame = new JFrame("Calculator");
    private final JPanel panel = new JPanel();
    private final JTextField textField = new JTextField();
    private final JButton[] buttons = new JButton[24];
    private final String[] btnTitles = {
        "%", "CE", "C", "<-",
        "1/x", "sqr", "sqrt", "÷",
        "7", "8", "9", "×",
        "4", "5", "6", "-",
        "1", "2", "3", "+",
        "+/-", "0", ".", "="
    };

    private double currentNumber = 0;
    private double firstNumber = 0;
    private String operator = "";

    private void initUI() {
        frame.setSize(450, 550);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        textField.setBounds(0, 30, 430, 60);
        textField.setFont(new Font("Arial", Font.PLAIN, 25));
        textField.setText("0");
        textField.setFocusable(false);
        textField.setHorizontalAlignment(SwingConstants.RIGHT);
        textField.setVisible(true);
        frame.add(textField);

        panel.setBounds(10, 100, 420, 400);
        panel.setBackground(Color.GRAY);
        panel.setLayout(new GridLayout(6, 4, 10, 10));
        panel.setVisible(true);
        frame.add(panel);

        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(btnTitles[i]);
            buttons[i].setFont(new Font("Arial", Font.PLAIN, 25));
            buttons[i].setVisible(true);
            buttons[i].setActionCommand(btnTitles[i]);
            buttons[i].addActionListener(this);
            panel.add(buttons[i]);
        }

        frame.setVisible(true);
    }

    public Calculator() {
        initUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        switch (command) {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if (textField.getText().equals("0")) {
                    textField.setText(command);
                } else {
                    textField.setText(textField.getText() + command);
                }
                currentNumber = Double.parseDouble(textField.getText());
                break;

            case ".":
                if (!textField.getText().contains(".")) {
                    textField.setText(textField.getText() + ".");
                }
                break;

            case "+":
            case "-":
            case "×":
            case "÷":
                firstNumber = currentNumber;
                operator = command;
                textField.setText("0");
                currentNumber = 0;
                break;

            case "=":
                switch (operator) {
                    case "+":
                        currentNumber = firstNumber + currentNumber;
                        break;
                    case "-":
                        currentNumber = firstNumber - currentNumber;
                        break;
                    case "×":
                        currentNumber = firstNumber * currentNumber;
                        break;
                    case "÷":
                        if (currentNumber != 0) {
                            currentNumber = firstNumber / currentNumber;
                            textField.setText(String.valueOf(currentNumber));
                        } else {
                            textField.setText("Cannot divide by zero");
                        }
                        firstNumber = 0;
                        currentNumber = 0;
                        operator = "";
                        return;
                }
                textField.setText(String.valueOf(currentNumber));
                firstNumber = 0;
                operator = "";
                break;

            case "C":
                textField.setText("0");
                currentNumber = 0;
                firstNumber = 0;
                operator = "";
                break;

            case "CE":
                textField.setText("0");
                currentNumber = 0;
                break;

            case "<-":
                String text = textField.getText();
                if (!text.isEmpty()) {
                    text = text.substring(0, text.length() - 1);
                    textField.setText(text.isEmpty() ? "0" : text);
                    currentNumber = text.isEmpty() ? 0 : Double.parseDouble(text);
                }
                break;

            case "1/x":
                if (currentNumber != 0) {
                    currentNumber = 1 / currentNumber;
                    textField.setText(String.valueOf(currentNumber));
                } else {
                    textField.setText("Cannot divide by zero");
                }
                break;

            case "sqr":
                currentNumber *= currentNumber;
                textField.setText(String.valueOf(currentNumber));
                break;

            case "sqrt":
                if (currentNumber >= 0) {
                    currentNumber = Math.sqrt(currentNumber);
                    textField.setText(String.valueOf(currentNumber));
                }
                break;

            case "+/-":
                currentNumber *= -1;
                textField.setText(String.valueOf(currentNumber));
                break;

            case "%":
                currentNumber /= 100;
                textField.setText(String.valueOf(currentNumber));
                break;
        }
    }

    public static void main(String[] args) {
        new Calculator();
    }
}